﻿using LoveEatRabbit.API.Write.Applications.Commands.Permissions;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Permissions
{
    public class UpdatePermissionCommandHandler : IRequestHandler<UpdatePermissionCommand, APIResult<int>>
    {
        private readonly IBaseRespository<PermissionModel> perRep;

        public UpdatePermissionCommandHandler(IBaseRespository<PermissionModel> perRep)
        {
            this.perRep = perRep;
        }
        public async Task<APIResult<int>> Handle(UpdatePermissionCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.修改成功;
            result.Message=APIResultCode.修改成功.ToString();

            var res = perRep.GetAll().FirstOrDefault(x=>x.PermissionId==request.PermissionId);
            res.PermissionURL=request.PermissionURL;
            res.PermissionName=request.PermissionName;
            res.Index=request.Index;
            await perRep.UpdateAsync(res);

            return await Task.FromResult(result);
        }
    }
}
